﻿using System.Data;
using System.Text;
using System.Windows.Forms;

public class ComboBoxTreeView : ComboBox
{
    private const int WM_LBUTTONDOWN = 0x201, WM_LBUTTONDBLCLK = 0x203;
    ToolStripControlHost treeViewHost;
    ToolStripDropDown dropDown;
    public ComboBoxTreeView()
    {
        TreeView treeView = new TreeView();
        treeView.AfterSelect += new TreeViewEventHandler(treeView_AfterSelect);
        treeView.BorderStyle = BorderStyle.None;
        treeView.CheckBoxes = true;
        treeViewHost = new ToolStripControlHost(treeView);
        dropDown = new ToolStripDropDown();
        dropDown.Width = this.Width;
        dropDown.Items.Add(treeViewHost);
    }
    public void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
        this.Text = TreeView.SelectedNode.Text .ToString ();
        dropDown.Close();
    }
    public TreeView TreeView
    {
        get { return treeViewHost.Control as TreeView; }
    }
    private void ShowDropDown()
    {
        if (dropDown != null)
        {
            treeViewHost.Size = new System.Drawing.Size(DropDownWidth - 2, DropDownHeight);
            dropDown.Show(this, 0, this.Height);
        }
    }
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN)
        {
            ShowDropDown();
            return;
        }
        base.WndProc(ref m);
    }

    private void InitializeComponent()
    {
            this.SuspendLayout();
            this.ResumeLayout(false);

    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (dropDown != null)
            {
                dropDown.Dispose();
                dropDown = null;
            }
        }
        base.Dispose(disposing);
    }
}
